﻿/********************************************************
** This simple plugin will force the bot using bandages
** when health is under 65% (editable in source)
**
** It checks if toon has First Aid skill, required lvl,
** and if right bandage is available, but doesn't
** check the skill lvl, so don't give him bandages
** that he can't use.
**
** Obviously, when not in combat, flying, on taxi, 
** Recently Bandaged, etc.
**
*******************************************************/

using System;
using System.Linq;

using Styx;
using Styx.CommonBot;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.TreeSharp;

namespace UseBandages
{
    public class UseBandages : HBPlugin
    {
        #region Globals
        public override string Name { get { return "UseBandages"; } }
        public override string Author { get { return "Neyder"; } }
        public override Version Version { get { return new Version(1, 0, 1); } }
        public override string ButtonText { get { return " Don't click this button ! "; } }
        public override bool WantButton { get { return true; } }
        private static LocalPlayer Me { get { return StyxWoW.Me; } }

        // You can edit this, dont forget to recompile
        public int maxLifePercent = 65; // Will apply bandage if life is below
        public int minLifePercent = 30; // Will let the bot eat if below
        #endregion

        // ------------------------------------------------------
        // lol (^.^)
        // ------------------------------------------------------
        public override void OnButtonPress()
        {
            Styx.Common.Logging.Write(System.Windows.Media.Color.FromRgb(255, 0, 0), "------------------------------");
            Styx.Common.Logging.Write(System.Windows.Media.Color.FromRgb(255, 0, 0), "--     !!! APOCALYPSE !!!   --");
            Styx.Common.Logging.Write(System.Windows.Media.Color.FromRgb(255, 0, 0), "------------------------------");
        }

        // ------------------------------------------------------
        // Very complex plugin initialisation
        // ------------------------------------------------------
        public UseBandages()
        {
             Styx.Common.Logging.Write(System.Windows.Media.Color.FromRgb(255, 255, 255), "UseBandages Plugin Loaded.");
        }

        // ------------------------------------------------------
        // Function from Singular.Helpers
        // ------------------------------------------------------
        public static bool HasItem(uint itemId)
        {
            return StyxWoW.Me.CarriedItems.Any(i => i.Entry == itemId);
        }

        /* ------------------------------------------------------
           Checking for each item because ItemInfo.ContainerClass 
           checking is broken (for bandages) at this moment...
           ------------------------------------------------------ */
        public static bool useBestBandage()
        {
                 if ((Me.Level == 90) && HasItem(72986)) Lua.DoString("UseItemByName(72986)"); // Heavy Windwool
            else if ((Me.Level >= 85) && HasItem(72985)) Lua.DoString("UseItemByName(72985)"); // Windwool
            else if ((Me.Level >= 75) && HasItem(53051)) Lua.DoString("UseItemByName(53051)"); // Dense Embersilk
            else if ((Me.Level >= 75) && HasItem(53050)) Lua.DoString("UseItemByName(53050)"); // Heavy Embersilk
            else if ((Me.Level >= 65) && HasItem(53049)) Lua.DoString("UseItemByName(53049)"); // Embersilk
            else if ((Me.Level >= 65) && HasItem(34722)) Lua.DoString("UseItemByName(34722)"); // Heavy Frostweave
            else if ((Me.Level >= 50) && HasItem(34721)) Lua.DoString("UseItemByName(34721)"); // Frostweave
            else if ((Me.Level >= 58) && HasItem(21991)) Lua.DoString("UseItemByName(21991)"); // Heavy Netherweave
            else if ((Me.Level >= 50) && HasItem(21990)) Lua.DoString("UseItemByName(21990)"); // Netherweave
            else if ((Me.Level >= 40) && HasItem(14530)) Lua.DoString("UseItemByName(14530)"); // Heavy Runecloth
            else if ((Me.Level >= 40) && HasItem(14529)) Lua.DoString("UseItemByName(14529)"); // Runecloth
            else if ((Me.Level >= 10) && HasItem(8545))  Lua.DoString("UseItemByName(8545)");  // Heavy Mageweave
            else if ((Me.Level >= 10) && HasItem(8544))  Lua.DoString("UseItemByName(8544)");  // Mageweave
            else if ((Me.Level >= 10) && HasItem(6451))  Lua.DoString("UseItemByName(6451)");  // Heavy Silk
            else if ((Me.Level >= 10) && HasItem(6450))  Lua.DoString("UseItemByName(6450)");  // Silk
            else if ((Me.Level >= 10) && HasItem(3531))  Lua.DoString("UseItemByName(3531)");  // Heavy Wool
            else if ((Me.Level >= 10) && HasItem(3530))  Lua.DoString("UseItemByName(3530)");  // Wool
            else if ((Me.Level >= 10) && HasItem(2581))  Lua.DoString("UseItemByName(2581)");  // Heavy Linen
            else if ((Me.Level >= 10) && HasItem(1251))  Lua.DoString("UseItemByName(1251)");  // Linen
            else return false;
            return true;
        }

        public override void Pulse()
        {
            if ((Me.IsValid && !Me.Combat && Me.IsAlive && !Me.IsGhost && !Me.IsOnTransport &&  !Me.OnTaxi && !Me.Stunned &&
               !(Me.Mounted && Me.IsFlying) && SpellManager.HasSpell("First Aid") && !Me.HasAura("Recently Bandaged") &&
               !Me.IsCasting && !Me.IsChanneling) && !Me.IsActuallyInCombat && !Me.IsCasting && Me.IsStealthed &&
               !StyxWoW.Me.IsDead && Me.HealthPercent < maxLifePercent && Me.HealthPercent > minLifePercent)
            {
                if (useBestBandage())
                {
                    Styx.Common.Logging.Write(System.Windows.Media.Color.FromRgb(0, 255, 0), "Applying bandage ! (At least trying)");
                    return;
                }
                else
                {
                    Styx.Common.Logging.Write(System.Windows.Media.Color.FromRgb(255, 0, 0), "No suitable bandage found :(");
                    return;
                }
            }
            else
            {
                return;
            }
        } // Pulse
    } // Class
}